﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.DomainObject.Technology;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.System;
using WMS.FC.Domain.Service.Technology.Interface;

namespace WMS.FC.Domain.Service.Technology
{
    public class EditTechnologyRouteProcessService : ServiceBase, IEditTechnologyRouteProcessService
    {
        protected readonly ITechnologyRepository technologyRepository;

        public EditTechnologyRouteProcessService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            ITechnologyRepository technologyRepository)
            : base(logger, options, mapper)
        {
            this.technologyRepository = technologyRepository;
        }

        public async Task<ActionResultBase> EditTechnologyRouteProcess(TechnologyRouteProcessDO? dO, UserInfo userInfo)
        {
            var result = new ActionResultBase("编辑工艺路径工序");
            try
            {
                if (dO == null)
                    return result.ArgumentError("工艺路径工序数据不能为空");

                var technologyRouteProcessPO = await technologyRepository.FindById<TechnologyRouteProcessPO>(dO.Id);
                if (technologyRouteProcessPO == null)
                    return result.BusinessError($"ID:{dO.Id},工艺路径工序数据未找到");

                var count = 0;
                if (technologyRouteProcessPO.SortNumber == dO.SortNumber)
                {
                    mapper.Map(dO, technologyRouteProcessPO);
                    count = await technologyRepository.Update(technologyRouteProcessPO, userInfo);
                }
                else
                {
                    var technologyRouteProcessPOList = await technologyRepository.FindByConditionOrderby<TechnologyRouteProcessPO, int>(entity =>
                    entity.TechnologyRouteCode == technologyRouteProcessPO.TechnologyRouteCode && entity.Id != technologyRouteProcessPO.Id, entity => entity.SortNumber);

                    mapper.Map(dO, technologyRouteProcessPO);
                    if (dO.SortNumber == null || dO.SortNumber <= 0 || dO.SortNumber > technologyRouteProcessPOList.Count + 1)
                    {
                        technologyRouteProcessPOList.Add(technologyRouteProcessPO);
                    }
                    else
                    {
                        technologyRouteProcessPOList.Insert(dO.SortNumber.Value - 1, technologyRouteProcessPO);
                    }

                    for (var i = 0; i < technologyRouteProcessPOList.Count; i++)
                    {
                        technologyRouteProcessPOList[i].SortNumber = i + 1;
                    }

                    count = await technologyRepository.Update(technologyRouteProcessPOList, userInfo);
                }

                if (count == 0)
                    return result.DbError("编辑工艺路径工序异常");

                result.Message = "编辑数据成功";
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
